package gui;

import net.BackClient;
import net.ReceiveInformation;
import util.Protocol;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class UserUI extends JFrame {

    private static final long serialVersionUID = 7331169995258952629L;
    BackClient backClient = new BackClient();
    Protocol protocol = new Protocol();
    String nickname;
    String friendNickname;
    JTextArea textArea;
    Thread thread = new Thread(new ReceiveInformation());
    ReceiveInformation information = new ReceiveInformation();
    private JPanel contentPane;
    private JTextField textField;

    public UserUI() {
        thread.start();
        setTitle("聊天界面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 650, 449);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 43, 614, 169);
        contentPane.add(scrollPane);

        textArea = new JTextArea();
        textArea.setEditable(false);
        Font f = new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 20);
        textArea.setFont(f);
        scrollPane.setViewportView(textArea);

        JButton sendbutton = new JButton("发送");
        sendbutton.addActionListener(arg0 -> {
            String friendName = friendNickname;
            String conversation = textField.getText();
            String from = "From;" + nickname + ",";
            String to = "To;" + friendName + ",";
            String massage = from + to + conversation;

            textArea.append(nickname + ":" + conversation + "\n");
            textField.setText("");

            String finalmassage = protocol.getToFriendMessage(massage);
            backClient.SendMassage(finalmassage);
        });
        sendbutton.setBounds(531, 362, 93, 23);
        contentPane.add(sendbutton);

        JButton exitNewButton = new JButton("关闭");
        exitNewButton.addActionListener(arg0 -> {
            backClient.SendMassage("close");
            setVisible(false);
        });
        exitNewButton.setBounds(378, 362, 93, 23);
        contentPane.add(exitNewButton);

        textField = new JTextField();
        textField.setBounds(10, 222, 614, 116);
        contentPane.add(textField);
        textField.setColumns(10);

        contentPane.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                UserUI frame = new UserUI();
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public void getFriendID(String friendID) {
        this.friendNickname = friendID;
        information.addThisWindows(friendNickname, this);
        JLabel lblNewLabel_1 = new JLabel(friendNickname);
        lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 20));
        lblNewLabel_1.setBounds(10, 18, 54, 15);
        contentPane.add(lblNewLabel_1);
        lblNewLabel_1.setVisible(true);
    }

    public void setConversation(String name, String content) {
        textArea.append(name + ":" + content + "\n");
    }
}


